﻿using System;
using System.Collections.Generic;

using Token.Piker.BLL;
using System.Drawing;
using System.Web.Security;
using Token.Piker.Common;

namespace Token.Piker.Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            AdminBLL adminBLL = new AdminBLL();
            List<Token.Piker.Model.Admin> userList = adminBLL.GetModelList("AdminName = '" + txtUserName.Text.Trim() + "'");
            if (userList.Count == 0)
            {
                txtUserName.Text = "用户名不正确";
                txtUserName.ForeColor = Color.Red;
                return;
            }
            else
            {
                Token.Piker.Model.Admin admin = userList[0];
                string password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");
                if (password != admin.AdminPwd)
                {
                    txtPassword.Text = "密码不正确";
                    txtPassword.ForeColor = Color.Red;
                    return;
                }
                if (admin.Flag == 0)
                {
                    txtUserName.Text = "您尚未通过审核";
                    txtUserName.ForeColor = Color.Red;
                    return;
                }
                //登录成功
                Session[GlobalConstants.PIKER_ADMIN] = admin.ID;
                //更新最近登录时间
                admin.LastLogTime = DateTime.Now;
                adminBLL.Update(admin);
                FormsAuthentication.RedirectFromLoginPage(admin.AdminName, false);
            }
        }
    }
}